package org.log4jeditor.xml.editor.utils;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
import org.eclipse.wst.sse.ui.internal.contentassist.ContentAssistUtils;
import org.eclipse.wst.xml.core.internal.parser.regions.AttributeValueRegion;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
import org.junit.Before;
import org.junit.Test;
import org.log4jconfig.xml.editor.utils.StructuredDocumentRegionUtils;
import org.log4jeditor.xml.editor.AbstractUITestCase;

@SuppressWarnings("restriction")
public class StructuredDocumentRegionUtilsTestCase extends AbstractUITestCase {

	@Before
	public void setup() throws Exception {
		setupEditorConfiguration("log4j-structureddocumentregionutils.xml");
	}
	
	@Test
	public void testExtractAttributeName() throws Exception {
		int offset = sourceViewer.getDocument().getLineOffset(3) + 45;
		IDOMNode node = (IDOMNode) ContentAssistUtils.getNodeAt(sourceViewer, offset);
		assertEquals("Wrong node name returned:", "appender", node.getNodeName());
		IStructuredDocumentRegion structuredDocumentRegion = ContentAssistUtils.getStructuredDocumentRegion(sourceViewer, offset);
		assertEquals("Unexpected attribute name", "class", StructuredDocumentRegionUtils.extractAttributeName(structuredDocumentRegion, (AttributeValueRegion)structuredDocumentRegion.getRegionAtCharacterOffset(structuredDocumentRegion.getStartOffset() + 40)));
	}

	@Test
	public void testExtractAttributeValueIStructuredDocumentRegionString() throws Exception {
		int offset = sourceViewer.getDocument().getLineOffset(3) + 45;
		IDOMNode node = (IDOMNode) ContentAssistUtils.getNodeAt(sourceViewer, offset);
		assertEquals("Wrong node name returned:", "appender", node.getNodeName());
		IStructuredDocumentRegion structuredDocumentRegion = ContentAssistUtils.getStructuredDocumentRegion(sourceViewer, offset);
		assertEquals("Unexpected attribute value", "org.apache.log4j.ConsoleAppender", StructuredDocumentRegionUtils.extractAttributeValue(structuredDocumentRegion, "class"));
	}

	@Test
	public void testExtractAttributeValueIStructuredDocumentRegionStringKOUnexistingAttributeName() throws Exception {
		int offset = sourceViewer.getDocument().getLineOffset(3) + 45;
		IDOMNode node = (IDOMNode) ContentAssistUtils.getNodeAt(sourceViewer, offset);
		assertEquals("Wrong node name returned:", "appender", node.getNodeName());
		IStructuredDocumentRegion structuredDocumentRegion = ContentAssistUtils.getStructuredDocumentRegion(sourceViewer, offset);
		assertNull("Unexpected attribute value", StructuredDocumentRegionUtils.extractAttributeValue(structuredDocumentRegion, "clazz"));
	}

	@Test
	public void testExtractAttributeValueIStructuredDocumentRegionInt() throws Exception {
		int offset = sourceViewer.getDocument().getLineOffset(3) + 45;
		IDOMNode node = (IDOMNode) ContentAssistUtils.getNodeAt(sourceViewer, offset);
		assertEquals("Wrong node name returned:", "appender", node.getNodeName());
		IStructuredDocumentRegion structuredDocumentRegion = ContentAssistUtils.getStructuredDocumentRegion(sourceViewer, offset);
		assertEquals("Unexpected attribute value", "org.apache.log4j.ConsoleAppender", StructuredDocumentRegionUtils.extractAttributeValue(structuredDocumentRegion, structuredDocumentRegion.getStartOffset() + 40));
	}

	@Test
	public void testExtractAttributeValueIStructuredDocumentRegionIntKOOutOfBounds() throws Exception {
		int offset = sourceViewer.getDocument().getLineOffset(3) + 45;
		IDOMNode node = (IDOMNode) ContentAssistUtils.getNodeAt(sourceViewer, offset);
		assertEquals("Wrong node name returned:", "appender", node.getNodeName());
		IStructuredDocumentRegion structuredDocumentRegion = ContentAssistUtils.getStructuredDocumentRegion(sourceViewer, offset);
		assertNull("Unexpected attribute value", StructuredDocumentRegionUtils.extractAttributeValue(structuredDocumentRegion, structuredDocumentRegion.getStartOffset() + 400));
	}

	@Test
	public void testExtractTagNameRegion() throws Exception {
		int offset = sourceViewer.getDocument().getLineOffset(3) + 45;
		IDOMNode node = (IDOMNode) ContentAssistUtils.getNodeAt(sourceViewer, offset);
		assertEquals("Wrong node name returned:", "appender", node.getNodeName());
		IStructuredDocumentRegion structuredDocumentRegion = ContentAssistUtils.getStructuredDocumentRegion(sourceViewer, offset);
		assertEquals("Unexpected tag name", "appender", StructuredDocumentRegionUtils.extractTagName(structuredDocumentRegion));
	}
	
	@Test
	public void testExtractTagNameRegionKoMalformedElement() throws Exception {
		int offset = sourceViewer.getDocument().getLineOffset(10) + 5;
		IStructuredDocumentRegion structuredDocumentRegion = ContentAssistUtils.getStructuredDocumentRegion(sourceViewer, offset);
		assertNull("Unexpected tag name", StructuredDocumentRegionUtils.extractTagName(structuredDocumentRegion));
	}

}
